package controller

import (
	bc "Agricultural/application/blockchain"
	"Agricultural/application/lib"
	"bytes"
	"encoding/json"
	"fmt"
	"github.com/gin-gonic/gin"
	"net/http"
)

// 加工农作物
func CreateMachining(ctx *gin.Context)  {
	// 解析结构体
	req := new(lib.Machining)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("createMachining", [][]byte{
		marshal,
		[]byte(req.Subjection),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 入仓
func InStock(ctx *gin.Context)  {
	// 解析结构体
	req := new(lib.Stock)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("inStock", [][]byte{
		marshal,
		[]byte(req.Subjection),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 出仓
func OutStock(ctx *gin.Context)  {
	subId := ctx.Query("subId")
	stockId := ctx.Query("stockId")
	resp, err := bc.ChannelExecute("outStock", [][]byte{
		[]byte(stockId),
		[]byte(subId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 创建订单
func CreateOrder(ctx *gin.Context)  {
	// 解析结构体
	req := new(lib.Order)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("createOrder", [][]byte{
		marshal,
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 添加物流信息
func AddLogistics(ctx *gin.Context)  {
	orderId := ctx.Query("orderId")
	// 解析结构体
	req := new(lib.Logistics)
	if err := ctx.ShouldBind(req); err != nil {
		_ = ctx.AbortWithError(http.StatusBadRequest, err)
		return
	}
	marshal, _ := json.Marshal(req)
	resp, err := bc.ChannelExecute("addLogistics", [][]byte{
		marshal,
		[]byte(orderId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 确认交易完成
func ConfirmOrder(ctx *gin.Context)  {
	orderId := ctx.Query("orderId")
	resp, err := bc.ChannelExecute("confirmOrder", [][]byte{
		[]byte(orderId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.JSON(http.StatusOK, resp)
}

// 查询订单
func QueryOrder(ctx *gin.Context)  {
	orderId := ctx.Query("orderId")
	resp, err := bc.ChannelExecute("queryOrder", [][]byte{
		[]byte(orderId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.String(http.StatusOK, bytes.NewBuffer(resp.Payload).String())
}

// 追溯订单
func TraceBackTo(ctx *gin.Context)  {
	orderId := ctx.Query("orderId")
	resp, err := bc.ChannelExecute("traceBackTo", [][]byte{
		[]byte(orderId),
	})
	if err != nil {
		fmt.Println("ERROR: ", err.Error())
		ctx.String(http.StatusInternalServerError, err.Error())
		return
	}
	ctx.String(http.StatusOK, bytes.NewBuffer(resp.Payload).String())
}